package zust.javaee.java;

/**
 * Created by liy on 2017/1/5.
 */

import java.util.Properties;

import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

/**
 * 本类测试简单邮件 直接用邮件发送
 *
 * @author Administrator
 */
public class sendEmail {

    public static void main(String args[]) throws MessagingException {
        JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
        // 设定mail server
        senderImpl.setHost("smtp.163.com");

        // 建立邮件消息
//        SimpleMailMessage mailMessage = new SimpleMailMessage();

        // 建立邮件消息,发送简单邮件和html邮件的区别
        MimeMessage mailMessagem = senderImpl.createMimeMessage();
        MimeMessageHelper mailMessage = new MimeMessageHelper(mailMessagem, true, "utf-8");

        // 设置收件人，寄件人 用数组发送多个邮件
        // String[] array = new String[] {"sun111@163.com","sun222@sohu.com"};
        // mailMessage.setTo(array);
        mailMessage.setTo("295573992@qq.com");
        mailMessage.setFrom("senselyan@163.com");
        mailMessage.setSubject("万社-学生社团管理系统");
        mailMessage.setText("<h1>点击如下链接完成注册</h1>" +
                "<p>http://127.0.0.1:8000/corporation/login?name=asd&password=A60C7F78C97F963F8B5B763303F8A</p>",
                true);

        senderImpl.setUsername("senselyan@163.com"); // 根据自己的情况,设置username
        senderImpl.setPassword("wodewangyi1109"); // 根据自己的情况, 设置password

//        Properties prop = new Properties();
        Properties prop = System.getProperties();
        prop.put("mail.smtp.auth", "true"); // 将这个参数设为true，让服务器进行认证,认证用户名和密码是否正确
        prop.put("mail.smtp.timeout", "25000");
        senderImpl.setJavaMailProperties(prop);


//        //指定SMTP服务器
//                 props.put("mail.smtp.host", SMTPHost);
//                 props.put("mail.smtp.auth", "true"); //true一定要加引号
        // 发送邮件
         senderImpl.send(mailMessagem);

        System.out.println(" 邮件发送成功.. ");
    }
}
